// pages/login/login.js
var app = getApp();
import user from '../../api/user.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    tabsIndex:1,
    btnAct:false,
    ifGetYzm:false,
    timeNum: 60,
    tipText:"",  //验证码已发送至XXXXX

    idCardNum:"",  //省份证号
    studentNum:"",  //学籍号
    phoneNum:"",   //电话号码
    yzmNum:"",  //验证码
	canIUse: wx.canIUse('button.open-type.getUserInfo'),
	headImg:"",
	nickName:"",
  },
  //获取微信用户信息
	loadwxUserinfo(){
		const that = this
		wx.getSetting({
        success: function(res){
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.getUserInfo({
              success: function(res) {
                console.log(res.userInfo)
				wx.setStorageSync('Wxuserinfo',res.userInfo)
				that.setData({
					headImg:res.userInfo.avatarUrl,
					nickName:res.userInfo.nickName
				})
              }
            })
          }
        }
      })
    },
    bindGetUserInfo: function(e) {
	    console.log(e.detail.userInfo)
		wx.setStorageSync('Wxuserinfo',e.detail.userInfo)
		this.setData({
			headImg:e.detail.userInfo.avatarUrl,
			nickName:e.detail.userInfo.nickName
		})
		if(parseInt(this.data.tabsIndex) === 1){
			 let data = {
				  idCard:this.data.idCardNum,
				  studentId:this.data.studentNum,
				  headImg:this.data.headImg,
				  nickName:this.data.nickName
			 }
			 this.logingo(data) 
		}else{
		  let data = {
			  phone:this.data.phoneNum,
			  code:this.data.yzmNum,
			  headImg:this.data.headImg,
			  nickName:this.data.nickName
		  }
		  this.logingo(data) 
		}
	},
  //登录
  logingo(data){
	  user.login(data).then(res =>{
		  if(res.data.errno === 0){
			  wx.setStorageSync('token',res.data.data.token)
			  wx.setStorageSync('usertype',res.data.data.loginType)
			  wx.setStorageSync('userinfo',res.data.data.userInfo)
			  wx.navigateTo({
			    url: "/pages/teacherIndex/teacherIndex",
			  })
		  }
	  })
  },
  // 返回
  goBack(e) {
    wx.navigateBack({
      delta: e.currentTarget.dataset.index
    })
  },
  // 点击跳转页面
  openTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  onLoginBtn(){
	  console.log(this.data.tabsIndex)
	  if(parseInt(this.data.tabsIndex) === 1){
		 let data = {
			  idCard:this.data.idCardNum,
			  studentId:this.data.studentNum,
			  headImg:this.data.headImg,
			  nickName:this.data.nickName
		 }
		 this.logingo(data) 
	  }else{
		  let data = {
			  phone:this.data.phoneNum,
			  code:this.data.yzmNum,
			  headImg:this.data.headImg,
			  nickName:this.data.nickName
		  }
		  this.logingo(data) 
	  }
	 
  },
  // 点击切换
  onTabsClick(e){
    this.setData({
      tabsIndex:e.currentTarget.dataset.index
    })
	wx.setStorageSync('logintype',this.data.tabsIndex)
  },
  // 获取省份证号
  getIdCardNum(e){
	  console.log(e)
    this.setData({
      idCardNum: e.detail.value
    })
    //this.data.studentNum.length > 0
    //&& this.data.studentNum.length > 0
    if (this.data.idCardNum.length > 0){
      this.setData({
        btnAct: true
      })
    }else{
      this.setData({
        btnAct: false
      })
    }
  },
  // 获取学籍号
  getStudentNum(e) {
    this.setData({
      studentNum: e.detail.value
    })
    //&& this.data.studentNum.length > 0
    if (this.data.idCardNum.length > 0 ) {
      this.setData({
        btnAct: true
      })
    } else {
      this.setData({
        btnAct: false
      })
    }
  },
  // 获取输入的手机号
  getPhoneNum(e){
	  if(e.detail.value.length === 11){
		 if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e.detail.value))){
		 	wx.showToast({title:'请输入正确手机号'})
		 	return
		 }else{
			this.setData({
				phoneNum: e.detail.value
			}) 
		 }
		  
	  }
		
  },
  // 获取输入的验证码
  getYzmNum(e){
    this.setData({
      yzmNum: e.detail.value
    })
  },
  // 点击获取验证码
  onGetYzm(e) {
    var that = this;
	let data = {
		mobile:this.data.phoneNum
	}
     user.regCaptcha(data).then(res =>{
		 if(res.data.errno === 0){
			 that.setData({
			   ifGetYzm: true,
			   tipText: "验证码已发送至" + that.data.phoneNum
			 })
			 var timeNum = that.data.timeNum;
			 that.setData({
			   timeNum: timeNum
			 })
			 var interval = setInterval(function () {
			   that.setData({
			     timeNum: (timeNum - 1)
			   })
			   timeNum--;
			   if (timeNum <= 0) {
			     clearInterval(interval)
			     that.setData({
			       ifGetYzm: false,
			       timeNum: 60,
			     })
			   }
			 }, 1000)
		 }
	 })
      
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	this.loadwxUserinfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})